<template>
	<view>
		<view :style="'height: '+uHeight+';width:'+uWidth+';'" class="grid col-1 grid-square flex-sub">
			<view :style="'height: '+uHeight+';width:'+uWidth+';'" class="bg-img" @tap="ViewImage" :data-url="images" v-if="images!=''">
				<image :style="'height: '+uHeight+';width:'+uWidth+';position:relative;'" :src="images" mode="scaleToFill"></image>
				<view v-if="!readOnly" class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
					<text class='cuIcon-close'></text>
				</view>
			</view>
			<view  v-if="!readOnly" :style="'line-height:'+uHeight+';height: '+uHeight+';width:'+uWidth+';'" :class="defImages==''?'solids icon-add':'bg-img'" @tap="ChooseImage" v-else>

				<text  v-if="defImages==''"   class="cuIcon-camera base-font-70"></text>
				<image :style="'height: '+uHeight+';width:'+uWidth+';position:relative;'" v-else :src="defImages" mode="aspectFit"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/utils/api.js';
	import siteApi from '@/utils/siteApi.js';
	export default {
		data() {
			return {
				uHeight: '',
				uWidth: '',
				images:''
			}
		},
		props: {
			readOnly:{
			 type: [Boolean,String],
			 default () {
					return false;
				}
			},
			height: {
			 type: String,
			 default () {
					return 200;
				}
			},
			width: {
				type: String,
				default () {
					return 200;
				}
			},
			defImages: {
				type: String,
				default () {
					return '';
				}
			},
			sImages: {
				type: String,
				default () {
					return '';
				}
			},
		},
		mounted() {
			if (this.$util.isNotEmpty(this.width)) {
				this.uHeight = uni.upx2px(this.height) + 'px';
			}
			if (this.$util.isNotEmpty(this.width)) {
				this.uWidth = uni.upx2px(this.width) + 'px';
			}
			this.images=this.sImages;
		},
		watch: {
		  sImages:{
		    handler(newValue, oldValue) {  
		      this.images=newValue;
		    },
		    deep:true
		  }
		},
		methods: {
			callBack() {
				this.$emit("change-images", this.images);
			},
			ChooseImage() {
				let _self = this;
				api.getAuthorize('scope.camera', "你拒绝了授权,无法调用拍照功能", function() {
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'],
						success: (res) => {
							api.fileUpload(_self.GLOBALVARIABLE.baseURL, res.tempFilePaths[0],
								function(result) {
									if (result.code == 0) {
										_self.images = result.data;
										_self.callBack();
									} else {
										uni.showModal({
											title: '提示',
											content: result.msg,
											showCancel: false,
											success: function(res) {}
										});
									}
								});
						}
					});
				});
			},
			ViewImage(e) {
				var imageList=[];
				imageList.push(this.images);
				uni.previewImage({
					urls: imageList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.images='';
							this.callBack();
						}
					}
				})
			},
		}
	}
</script>

<style>
	.icon-add{
		text-align: center;padding-bottom:0px !important;
		background-color: #F7F7F7;
	}
</style>
